import com.mit.web.business.proxy.MaintenanceProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.Constants;
import com.mit.web.config.SizeGroupConfig;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.util.FlexUtil;
import com.mit.web.util.LogUtil;
import com.mit.web.util.MessageUtil;
import com.mit.web.util.Validator;
import com.mit.web.util.FlexSession;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.vo.maintenance.OrganizationVO;

import flash.events.MouseEvent;
import flash.geom.Point;

import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.logging.ILogger;
import mx.resources.ResourceManager;
import mx.rpc.events.ResultEvent;
import mx.utils.StringUtil;

private var _logger:ILogger;
[Bindable]private var _orgVo:OrganizationVO = new OrganizationVO();
private var _proxy:MaintenanceProxy = MaintenanceProxy.getInstance();
private var _pm:IProgressMonitor;


private function init():void{
	_logger = LogUtil.getLogger(this);
	_logger.debug("initialize organization windows");
	
	doEnquiry();
}

/*********** Business Method ************/
private function doEnquiry():void{
	_logger.debug("id=" + FlexSession.currentUser.id);
	var responder:IInvokeResponder = _proxy.retrieveOrganization(FlexSession.currentUser.id).installProgressBar(_pm, this);
	responder.addResultListener(retrieveOrganizationHandler);
	responder.addFaultListener(exceptionHandler);
}

private function retrieveOrganizationHandler(event:ResultEvent):void{
	_orgVo = event.result as OrganizationVO;	
	_logger.debug("name: " + _orgVo.name + ", fullName=" + _orgVo.fullName
		+ ", lastUpdateTime=" + _orgVo.lastUpdatedTime);
}

private function doSave(event:MouseEvent):void{
	if(mx.validators.Validator.validateAll(validators).length == 0 ){
		// validate data
		if(!validate()) {
			return;
		}
		
		copyProperties();
		var responder:IInvokeResponder;
		responder = _proxy.updateOrganization(_orgVo).installProgressBar(_pm, this);
		responder.addFaultListener(exceptionHandler);
		responder.addResultListener(savedHandler);
	} else {
		event.preventDefault();
	}
}

private function savedHandler(event:ResultEvent):void{
	_orgVo = event.result as OrganizationVO;

	var message:String = ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"info.common.saved",["组织设定"]);
	showMessageTip(message, this);
}

private function copyProperties():void {
	_logger.debug("image fileName: " + _uploader.wrappedFileName);
	_orgVo.name = txt_name.text;
	_orgVo.fullName = txt_fullName.text;
	_orgVo.address = txt_address.text;
	_orgVo.tel = txt_tel.text;
	_orgVo.fax = txt_fax.text;
	_orgVo.mobile = txt_mobile.text;
	_orgVo.email = txt_email.text;
	_orgVo.image = _uploader.wrappedFileName;
}

private function validate():Boolean{
	var rtn:Boolean = true;
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_name.text)){
		showRequiredErrorTip(txt_name);
		rtn = false;
	}
//	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_email.text)){
//		showRequiredErrorTip(txt_email);
//		rtn = false;
//	}
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_fullName.text)){
		showRequiredErrorTip(txt_fullName);
		rtn = false;
	}
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_mobile.text)){
		showRequiredErrorTip(txt_mobile);
		rtn = false;
	}
	
	return rtn;
}